1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.io.*;
32
33 public class ReadLine {
34
35 public static void main(String[] args) throws IOException {
36
37
38 BufferedReader reader;
39 String[][] strings = {
40 {"CR/LF\r\n", "CR/LF"},
41 {"LF-Only\n", "LF-Only"},
42 {"CR-Only\r", "CR-Only"},
43 {"CR/LF line\r\nMore data", "More data"}
44 };
45
46
47
48
49 for (int i = 0; i < 3; i++) {
50 reader = new BufferedReader(new
51 BoundedReader(strings[i][0]), strings[i][0].length());
52 if (!reader.readLine().equals(strings[i][1]))
53 throw new RuntimeException("Read incorrect text");
54 }
55
56
57
58
59 markResetTest("Lot of textual data\rMore textual data\n",
60 "More textual data");
61
62
63 markResetTest("Lot of textual data\r\nMore textual data\n",
64 "More textual data");
65
66
67 markResetTest("Lot of textual data\nMore textual data\n",
68 "More textual data");
69
70
71
72
73
74
75 reader = new BufferedReader(new
76 BoundedReader(strings[3][0]), strings[3][0].length());
77 reader.readLine();
78 if (reader.read() != 'M')
79 throw new RuntimeException("Read() failed");
80
81
82
83
84
85
86 reader = new BufferedReader(new
87 BoundedReader(strings[3][0]), strings[3][0].length());
88 reader.readLine();
89
90 char[] buf = new char[9];
91 reader.read(buf, 0, 9);
92 String newStr = new String(buf);
93 if (!newStr.equals(strings[3][1]))
94 throw new RuntimeException("Read(char[],int,int) failed");
95 }
96
97 static void markResetTest(String inputStr, String resetStr)
98 throws IOException {
99 BufferedReader reader = new BufferedReader(new
100 BoundedReader(inputStr), inputStr.length());
101 System.out.println("> " + reader.readLine());
102 reader.mark(30);
103 System.out.println("......Marking stream .....");
104 String str = reader.readLine();
105 System.out.println("> " + str);
106 reader.reset();
107 String newStr = reader.readLine();
108 System.out.println("reset> " + newStr);
109
110
111 if (!newStr.equals(resetStr))
112 throw new RuntimeException("Mark/Reset failed");
113 }
114
115
116 private static class BoundedReader extends Reader{
117
118 private char[] content;
119 private int limit;
120 private int pos = 0;
121
122 public BoundedReader(String content) {
123 this.limit = content.length();
124 this.content = new char[limit];
125 content.getChars(0, limit, this.content, 0);
126 }
127
128 public int read() throws IOException {
129 if (pos >= limit)
130 throw new RuntimeException("Read past limit");
131 return content[pos++];
132 }
133
134 public int read(char[] buf, int offset, int length)
135 throws IOException
136 {
137 int oldPos = pos;
138 for (int i = offset; i < length; i++) {
139 buf[i] = (char)read();
140 }
141 return (pos - oldPos);
142 }
143
144 public void close() {}
145 }
146
147 }